<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/common/common.jsp" %>
<%@ include file="/common/grid/jqGrid.jsp" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    <title></title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="">
	<meta http-equiv="description" content="">
	
  </head>
  
  <body>
  	<center>
    	<s:form name="frm" id="frm" method="post" action="" namespace="">
    		<s:hidden id="listId" name="ld.id"></s:hidden>
    		<s:hidden id="moduleId" name="ld.listModule.id"></s:hidden>
    		
    		<table width="98%" cellspacing="1" cellpadding="3" bgcolor="#CCCCCC">
    			<tr bgcolor="#FFFFFF">
    				<td width="8%" align="right" class="label">
    					列表编码：
    				</td>
    				<td width="18%" align="left">
    					<s:textfield name="ld.listCode" id="listCode" cssStyle="width:98%" maxlength="20"></s:textfield>
    				</td>
    				<td width="8%" align="right" class="label">
    					列表名称：
    				</td>
    				<td width="18%" align="left">
    					<s:textfield name="ld.listName" id="listName" cssStyle="width:98%" maxlength="20"></s:textfield>
    				</td>
    				<td width="8%" align="right" class="label">
    					每页条数：
    				</td>
    				<td width="16%" align="left">
    					<s:select list="#{0:0, 5:5, 15:15, 25:25, 50:50, 100:100}" name="ld.pageSize" id="pageSize" cssStyle="width:80px"></s:select>
    				</td>
    				<td rowspan="3" width="24%" align="left" valign="top">
    					<s:if test='ld.id != null'>
    					<input type="button" value="新增" class="btn" onclick="addListField()" />
    					<input type="button" value="删除" class="btn" onclick="delListField()" />
    					<input type="button" value="保存排序" class="btn" onclick="saveSort()" />
    					<table id="listGrid" cellpadding="3" cellspacing="0"></table>
    					</s:if>
    				</td>
    			</tr>
    			<tr bgcolor="#FFFFFF">
    				<td align="right" class="label">
    					是否多选：
    				</td>
    				<td align="left">
    					<s:select list='#{"N":"否","Y":"是"}' name="ld.hasCheckbox" id="hasCheckbox" cssStyle="width:80px"></s:select>
    				</td>
    				<td align="right" class="label">
    					列表宽度：
    				</td>
    				<td align="left">
    					<s:textfield name="ld.listWidth" id="listWidth" cssStyle="width:98%" maxlength="8"></s:textfield>
    				</td>
    				<td align="right">
    					
    				</td>
    				<td align="left">
    					
    				</td>
    			</tr>
    			<tr bgcolor="#FFFFFF">
    				<td align="right" class="label">
    					sql脚本：
    				</td>
    				<td align="left" colspan="5">
    					<s:textarea name="ld.sqlStr" id="sqlStr" cssStyle="width:100%; height:300px"></s:textarea>
    				</td>
    			</tr>
    		</table>
    		
    		<table width="100%" cellspacing="0">
    			<tr>
    				<td align="center" valign="bottom" height="30">
    					<input type="button" value="保存" class="btn" onclick="addListDefine()" />
    					<input type="button" value="返回" class="btn" onclick="returnBack()" />
    				</td>
    			</tr>
    		</table>
    		
    	</s:form>
  	</center>
    
    <script type="text/javascript">
    $(document).ready(function(){
        var listId = $('#listId');
        var cName = new Array('字段名称', '排序');
    	var cModel = [
    					{name:'chineseName', index:'chineseName', width:200, align:'center', sortable:false, formatter:nameOption},
    					{name:'sort', index:'sort', width:60, align:'center', sortable:false, formatter:sortOption}
                      ];
        newJqGrid({
        	id: 'id',
        	gridId: 'listGrid',
        	pagerId: null,
        	url: 'inner/core/listFieldManageList.action?lvvo.listId=' + listId.val(),
        	head: cName,
        	column: cModel,
        	width: 260,
        	height: 'auto',
        	hasCb: true,
        	pageSize: 1000,
        	title: '字段列表'
        });
        
    });
    
    function addListDefine()
    {
        var moduleId = $('#moduleId');
        var listCode = $('#listCode');
        var listName = $('#listName');
        if(!isNotNull(listCode.val()))
        {
			showMsg('列表编码不能为空。', function(){
				listCode.focus();
			});
			return;
        }
        if(listCode.val().indexOf('_') > -1)
        {
			showMsg('列表编码不能有“_”。', function(){
				listCode.focus();
			});
			return;
        }
        if(!isNotNull(listName.val()))
        {
			showMsg('列表名称不能为空。', function(){
				listName.focus();
			});
			return;
        }
		var url = 'inner/core/saveListDefine.action';
		ajaxAction(url, 'post', 'frm', true, function(msg){
			var obj = eval(msg);
			if('success' == obj.result)
			{
				var listId = $('#listId');
				if(!isNotNull(listId.val()))
				{
					var forward = realPath + '/inner/core/toSaveListDefine.action?ld.id=' + obj.id;
					window.location.href = forward;
				}
			}
			else
			{
				showMsg(obj.message);
			}
		});
    }

    function returnBack()
    {
        var moduleId = $('#moduleId');
		var url = realPath + '/inner/core/listDefineManage.action?lvvo.moduleId=' + moduleId.val();
		window.location.href = url;
    }

    function nameOption(cellVal, options, row)
    {
        return '<a style="color:blue; text-decoration:underline" href="javascript:toSaveListField(\'' + row['id'] + '\')">' + row['chineseName'] + '</a>';
    }

    function sortOption(cellVal, options, row)
    {
        return '<s:textfield id="sort_' + row['id'] + '" name="fieldSort" cssStyle="width:50px; text-align:center" value="' + row['sort'] + '"></s:textfield>';
    }

    function toSaveListField(id)
    {
    	var url = realPath + '/inner/core/toSaveListField.action?lf.id=' + id;
        openWin(url, 850, 500, 'yes', 'yes');
    }

    function addListField()
    {
    	var listId = $('#listId');
        var url = realPath + '/inner/core/toSaveListField.action?lf.listDefine.id=' + listId.val();
        openWin(url, 850, 500, 'yes', 'yes');
    }

    function delListField()
    {
        if(hasCheck('jqg_listGrid') == false)
            return;
        showConfirm('确认删除？', function(v){
			if(v == true)
			{
				var url = 'inner/core/delListField.action';
		        ajaxAction(url, 'post', 'frm', false, function(){
					searchThis();
		        });
			}
        });
    }

    function searchThis()
    {
    	toSearchList('listGrid', null);
    }

    function saveSort()
    {
        var fieldCb = $('input:checkbox[name="jqg_listGrid"]');
        var fieldSort = $('input:text[name="fieldSort"]');
        var len = fieldSort.length;
        for(var i = 0; i < len; i++)
        {
			var sortx = $(fieldSort.get(i));
			if(checkInteger(sortx) == false)
			{
				showMsg('请确认所有排序都为整数');
				return;
			}
        }
        fieldCb.attr('checked', true);
        var url = 'inner/core/batchSaveSort.action';
        ajaxAction(url, 'post', 'frm', false, function(){
			searchThis();
        });
    }
    </script>
  </body>
</html>
